<!-- 新建项目 弹出层 新建项目 start -->
<style type="text/css">
div.dingdanBot input.submit[disabled]{background:#ddd;}
</style>
<div id="xiangmuShadow" class="o dn pr addXiangmuShadow">
    <div class="dingdanShadowBox palt">
        <div class="dingdanShadowBoxBox o">
        <form id="comCreateProject">
            <div class="dingdanTop" style="padding-bottom:0px;">
                <dl class="dingdanCom pr">
                    <dt class="palt">*</dt>
                    <dd class="text w120 o">
                        <span class="ib title fl">项目名称：</span>
                        <input type="text" name="name" class="fl txt" placeholder="点击输入">
                    </dd>
                </dl>
                <dl class="dingdanCom pr">
                    <dt class="palt">*</dt>
                    <dd class="text o pr">
                        <span class="ib title fl">项目金额：</span>
                        <input type="text" name="amount" class="fl txt" placeholder="点击输入">
                        <span class="part yuan">元</span>
                    </dd>
                </dl>
                <dl class="dingdanCom diandanTime pr">
                    <dt class="palt">*</dt>
                    <dd class="text o pr">
                        <span class="ib title fl">计划开工日期：</span>
                        <input type="text" class="fl txt" name="begin_time" id="begin_time" placeholder="请选择竣工日期">
                        <span class="part yuan"></span>
                    </dd>
                </dl>
                <dl class="dingdanCom diandanTime pr">
                    <dt class="palt">*</dt>
                    <dd class="text o pr">
                        <span class="ib title fl">计划竣工日期：</span>
                        <input type="text" class="fl txt" name="end_time" id="end_time" placeholder="请选择竣工日期">
                        <span class="part yuan"></span>
                    </dd>
                </dl>
                <script type="text/javascript">
                (function(){
                    layui.use('laydate', function(){
                        var laydate = layui.laydate;
                        laydate.render({
                            elem: '#begin_time', 
                            format: 'yyyy-MM-dd'
                        });
                        laydate.render({
                            elem: '#end_time', 
                            format: 'yyyy-MM-dd'
                        });
                    });
                })();
                </script>
                <style type="text/css">
                .selectPlace{width:585px; margin:0px auto;}
                .selectPlace dd.selectProvince{width:230px; position:absolute; top:0px; left:0px;}
                .selectPlace dd.selectProvince select.comSelect{width:130px; height:35px; position:absolute; top:0px; right:0px; border:1px solid #3296FA; font-size:16px; color:#3296FA; text-indent:5px; cursor:pointer;}
                </style>                
                <div class="selectPlace">
                    <dl class="dingdanCom pr">
                        <dt class="palt">*</dt>
                        <dd class="text w120 c selectProvince">
                            <span class="ib title fl">选择项目管理员：</span>
                            <select name="department" class="comSelect" style="width:200px; left:140px;">
                                <option value="请选择">请选择</option> 
                                <foreach name="commonParam.department" item="v">
                                <option  value="{$v.id}">{$v.name}</option>
                                </foreach>
                            </select>
                        </dd>
                        <dd class="text w120 c selectProvince" style="left:260px;">
                            <select class="comSelect dn" name="user_id"></select>
                        </dd>                                          
                    </dl>
                </div>
                <div class="selectPlace">
                    <dl class="dingdanCom pr">
                        <dt class="palt">*</dt>
                        <dd class="text w120 c selectProvince">
                            <span class="ib title fl">项目地点：</span>
                            <select name="province" class="comSelect">
                                <option value="请选择省">请选择省</option>
                            </select>
                        </dd>
                        <dd class="text w120 c selectProvince" style="left:160px;">
                            <select name="city" class="dn comSelect" >
                            </select>                                  
                        </dd>
                        <dd class="text w120 c selectProvince" style="left:320px;">
                            <select name="area" class="dn comSelect"></select>
                        </dd>                                                        
                    </dl>
                </div>
                <dl class="dingdanCom dingdanTextarea pr" style="height:100px;">
                    <dt class="palt">*</dt>
                    <dd class="text w120 o">
                        <span class="ib title fl">详细地址：</span>
                        <textarea class="layui-textarea" name="address" placeholder="点击输入合同详细地址" style="height:30px;"></textarea> 
                    </dd>
                </dl>
                <dl class="dingdanCom dingdanTextarea pr">
                    <dt class="palt">*</dt>
                    <dd class="text w120 o">
                        <span class="ib title fl">项目内容：</span>
                        <textarea class="layui-textarea" name="content" placeholder="点击输入合同内容详情"></textarea> 
                    </dd>
                </dl>                
            </div>
            <div class="dingdanBot" style="padding-bottom:20px;">
                <input type="button" class="submit" value="确认新建订单">
            </div>
        </div>
        </form>
    </div>
</div>
<!-- 新建项目 弹出层 end -->
<script type="text/javascript">
(function(){
var hostUrl = "http://"+"<?php echo $_SERVER['HTTP_HOST']; ?>/";
var ajaxUrl = hostUrl+'service/address'; // 请求地址
var ajaxUserUrl = hostUrl+'user/getlist'; // 请求地址
var ajaxCreateProjectUrl = hostUrl+"/project/create"; //创建项目地址
// comCreateProject                
var provinceList = ajaxGet(ajaxUrl,{},function(res){
    $("form#comCreateProject").find("select[name='province']").append(eachStr(res.data,1));
});
$("form#comCreateProject").find("select[name='province']").change(function(){
    ajaxGet(ajaxUrl,{id:$(this).val()},function(res){
        $("form#comCreateProject").find("select[name='city']").show().html(eachStr(res.data)).show();
        ajaxGet(ajaxUrl,{id:res.data[0].id},function(res){
            $("form#comCreateProject").find("select[name='area']").show().html(eachStr(res.data));
        });
    })
});
$("form#comCreateProject").find("select[name='city']").change(function(){
    ajaxGet(ajaxUrl,{id:$(this).val()},function(res){
        $("form#comCreateProject").find("select[name='area']").show().html(eachStr(res.data));
    });
});
$("form#comCreateProject").find("select[name='department']").change(function(){
    ajaxGet(ajaxUserUrl,{department_id:$(this).val()},function(res){
        if(res.data.length > 0){
            $("form#comCreateProject").find("select[name='user_id']").show().html(eachStr(res.data));
        }else{
            $("form#comCreateProject").find("select[name='user_id']").hide().html('');
        };
    });
});
$("form#comCreateProject").find("input.submit").click(function(){
    if(getValue("input[name='name']") == ""){
        layer.msg('项目名称不能为空');
        focusValue("input[name='name']");
        return false;
    };
    if(getValue("input[name='amount']") == ""){
        layer.msg('项目金额不能为空');
        focusValue("input[name='amount']");
        return false;
    };
    if(isNaN(getValue("input[name='amount']"))){
        layer.msg('项目金额必须是数字');
        focusValue("input[name='amount']");
        return false;
    };
    if(getValue("input[name='begin_time']") == ""){
        layer.msg('请选择项目计划开工日期'); 
        return false;
    };
    if(getValue("input[name='end_time']") == ""){
        layer.msg('请选择项目计划竣工日期'); 
        return false;
    };
    if(getValue("select[name='department']") == "请选择"){
        layer.msg('请选择项目管理员');
        return false;
    };
    if(getValue("select[name='province']") == "请选择省"){
        layer.msg('请选择项目地点');
        return false;
    };
    if(getValue("textarea[name='address']") == ""){
        layer.msg('请输入项目详细地址');
        focusValue("textarea[name='address']");
        return false;
    };
    if(getValue("textarea[name='content']") == ""){
        layer.msg('请输入项目内容');
        focusValue("textarea[name='content']");
        return false;
    };
    function getValue(curInput){
        return $("form#comCreateProject").find(curInput).val();
    };
    function focusValue(curInput){
        $("form#comCreateProject").find(curInput).focus();
    };
    $(this).attr('disabled','true');
    var serializeArray = $("form#comCreateProject").serializeArray();
    $.post(ajaxCreateProjectUrl,serializeArray,function(res){        
        if(res.code == 0){  
           layer.msg('创建成功', {icon:1});
           $(this).attr('disabled','false');
           setTimeout(function(){
                location.href = hostUrl+"Project/detail/id/"+res.data.id;
           },1000);
        }else{
            layer.msg(res.message,{icon:2});
           $(this).attr('disabled','false');
        }
    },'json');
    return false;
});
function eachStr(data,num =''){
    var dataStr = '';
    $.each(data,function(index,item){
        if(index == 0 && num == ''){
            dataStr +=  "<option value='"+item['id']+"' selected>"+item.name+"</option>";
        }else{
            dataStr +=  "<option value='"+item['id']+"'>"+item.name+"</option>";
        };
    });
    return dataStr;
};
function ajaxGet(url,data,fun){
    $.get(url,data,function(res){
        fun(res);
    },'json');
};
})();
</script>
<script type="text/javascript">
// 新建项目
function createXiangmu(){
    layer.open({
        type: 1,
        scrollbar:false,
        title: ['新建项目', 'font-size:18px;'],
        skin: 'layui-layer-rim', //加上边框
        area: ['650px', '650px'], //宽高
        content: $(".addXiangmuShadow")
    });
};
</script>